package com.shop.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.MerchantCategory;
import com.shop.mapper.MerchantCategoryMapper;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (MerchantCategory)表服务接口
 *
 * @author makejava
 * @since 2020-11-21 09:47:56
 */
@Service
@AllArgsConstructor
public class MerchantCategoryService {
    private final MerchantCategoryMapper merchantCategoryMapper;

    public Rsp getByMerchantId(int id) {
       List<MerchantCategory> merchantGoods=merchantCategoryMapper.getCategoryGoodsListByMerchantId(id);

        return Rsp.success(merchantGoods);
    }

    /**
     * 查询商品列表
     * @return
     */
    public List<MerchantCategory> getCategoryList(Integer merchantId) {
        return merchantCategoryMapper.getCategoryList(merchantId);
    }
    /**
     * @author Clh
     * @description 开启分页 模糊查询
     * @date  2020/11/25 19:55
     * @param pageParam 分页插件
     * @return com.github.pagehelper.PageInfo
    **/
    public PageInfo searchSomeByPage(PageParam<MerchantCategory> pageParam,Integer merchantId) {
            pageParam.getModel().setMerchantId(merchantId);
            //开启分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<MerchantCategory> merchantCategoryList = merchantCategoryMapper.searchSomeByPage(pageParam.getSuperSearchKeyWord(),pageParam.getModel());
            PageInfo<MerchantCategory> pageInfo = new PageInfo(merchantCategoryList);
            return pageInfo;
        }


    /**
     * 修改商品
     * @param merchantCategory
     * @return
     */
    public Integer updateCategoryName(MerchantCategory merchantCategory,Integer userId) {
        merchantCategory.setUpdateUserId(userId);
        return merchantCategoryMapper.update(merchantCategory);
    }

    /**
     * 新增商品
     * @param merchantCategory
     * @return
     */
    public int insertCategory(MerchantCategory merchantCategory,Integer merchantId) {
        merchantCategory.setMerchantId(merchantId);
        return merchantCategoryMapper.insert(merchantCategory);
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    public int delCategory(Integer id) {
        return merchantCategoryMapper.deleteById(id);
    }
    /**
     * @author Clh
     * @description 动态添加
     * @date  2020/11/25 19:34
     * @param merchantCategory 实体类
     * @param merchantId 店铺id
     * @return com.shop.model.vo.Rsp
    **/
    public Rsp insertSelective(MerchantCategory merchantCategory,Integer merchantId,Integer userId){
        merchantCategory.setMerchantId(merchantId);
        merchantCategory.setCreateUserId(userId);
        Integer insertSelective = merchantCategoryMapper.insertSelective(merchantCategory);
        return insertSelective>0?Rsp.success(null).msg("添加成功"):Rsp.fail().msg("添加失败");
    }
}
